<template>
	<view>
		<view class="search-box">	
			<uni-search-bar @input="input" :radius="100" cancelButton="none" focus></uni-search-bar>
		</view>
		<!-- 搜索结果 -->
		<scroll-view :style="{height: wh + 'px'}" scroll-y>
			<view class="result-list" v-if="searchList.length > 0">
				<view class="result-item" v-for="item in searchList" :key="item.goods_id" @click="goToDetail(item.goods_id)">
					<view class="goods_name">{{item.goods_name}}</view>
					<uni-icons type="arrowright" size="16"></uni-icons>
				</view>
			</view>
			<!-- 搜索历史 -->
			<view class="history-box" v-else>
				<view class="history-title">
					<view>搜索历史</view>
					<uni-icons type="trash-filled" @click="cleanHistory"></uni-icons>
				</view>
				<view class="history-list">
					<uni-tag v-for="(item,index) in showHistoryList" :key="index" :text="item" @click="goToGoodsList(item)"></uni-tag>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				kw: '',
				searchList: [],
				historyList: JSON.parse(uni.getStorageSync('kw') || '[]'),
				wh: 0
			}
		},
		computed: {
			showHistoryList() {
				return [...this.historyList].reverse()
			}
		},
		onLoad() {
			this.wh = uni.getSystemInfoSync().windowHeight - 50
		},
		methods: {
			cleanHistory() {
				this.historyList = []
				uni.clearStorageSync('kw')
			},
			// 保存搜索历史
			saveSearchHistory() {
				// this.historyList.push(this.kw)
				const set = new Set(this.historyList)
				// 调用set的两个方法，将最新的值展示到最前面
				set.delete(this.kw)
				set.add(this.kw)
				this.historyList = Array.from(set)
				// 本地存储
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			goToGoodsList(k) {
				uni.navigateTo({
					url: `/subpkg/goods-list/goods-list?query=${k}`
				})
			},
			goToDetail(goods_id) {
				uni.navigateTo({
					// url: '/subpkg/goods-detail/goods-detail' + '?goods_id=' + goods_id
					url: `/subpkg/goods-detail/goods-detail?goods_id=${goods_id}`
				})
			},
			input(e) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.kw = e
					this.getSearchList()
				},500)
			},
			async getSearchList() {
				if (this.kw === '') {
					this.searchList = []
					return
				}
				const {data: {message,meta}} = await uni.$http.get('/api/public/v1/goods/qsearch',{
					query: this.kw
				})
				if (meta.status !== 200) return uni.$showMsg()
				this.searchList = message
				this.saveSearchHistory()
			}
		}
	}
</script>

<style lang="scss">
.search-box {
	position: sticky;
	top: 0;
}
.result-list {
	padding: 0 5px;
	
	.result-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 13px 0;
		font-size: 12px;
		
		.goods_name {
			overflow: hidden;
			white-space: nowrap;
			text-overflow: ellipsis;
			margin-left: 3px;
		}
	}
}
.history-box {
	padding: 0 5px;
	.history-title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 40px;
		font-size: 13px;
	}
	.history-list {
		display: flex;
		flex-wrap: wrap;
		
		.uni-tag {
			margin: 5px  5px 0 0;
		}
	}
}
</style>
